﻿using System.Windows;
using Gavel.Common.Component.Helper;
using Gavel.Common.Utilities.Const;
using Gavel.Common.Utilities.Model;
using Gavel.ModLink.Helper;
using Gavel.ModLink.Model;
using Gavel.ModLink.ViewModel;

namespace Gavel.ModLink.Views;

/// <summary>
/// 寄存器写入视图交互逻辑
/// </summary>
public partial class RegisterWriteView
{
    /// <summary>
    /// 寄存器写入视图模型
    /// </summary>
    private readonly RegisterWriteViewModel _registerWriteViewModel;

    /// <summary>
    /// 寄存器写入视图
    /// </summary>
    public RegisterWriteView()
    {
        InitializeComponent();
        _registerWriteViewModel = (RegisterWriteViewModel)DataContext;
        ClientHelper.Instance.ReceivedEvent += Received;
        ClientHelper.Instance.SocketStatusChangedEvent += StatusChanged;
    }

    /// <summary>
    /// 预置值点击事件
    /// </summary>
    /// <param name="sender">事件发送者</param>
    /// <param name="e">事件参数</param>
    private async void WriteClick(object sender, RoutedEventArgs e)
    {
        try
        {
            WriteMessage message = new WriteMessage
            {
                Slave = _registerWriteViewModel.Slave,
                Address = _registerWriteViewModel.AddressDecimal,
                Value = _registerWriteViewModel.RegisterValue,
                Protocol = _registerWriteViewModel.Protocol,
                Function = _registerWriteViewModel.Function.Key,
                Endian = _registerWriteViewModel.Endian
            };

            MessageTransmit transmit = new MessageTransmit(message.Protocol, message.Function, TransmitWay.Request, message.Address, message.Message);
            await ClientHelper.Instance.Send(transmit);
        }
        catch (Exception exception)
        {
            NotifyHelper.Instance.Error(exception.Message);
        }
    }

    /// <summary>
    /// 取消点击事件
    /// </summary>
    /// <param name="sender">事件发送者</param>
    /// <param name="e">事件参数</param>
    private void CancelClick(object sender, RoutedEventArgs e) => _registerWriteViewModel.ShowView = Visibility.Collapsed;

    /// <summary>
    /// 显示预置弹窗
    /// </summary>
    /// <param name="value">寄存器值</param>
    /// <param name="optionsViewModel">寄存器配置视图模型</param>
    public void ShowWriteDialog(RegisterValue value, OptionsViewModel optionsViewModel)
    {
        _registerWriteViewModel.Slave = optionsViewModel.Slave;
        _registerWriteViewModel.Protocol = optionsViewModel.Protocol.Key;
        _registerWriteViewModel.Endian = optionsViewModel.Endian;
        _registerWriteViewModel.AddressDecimal = value.AddressDecimal;
        _registerWriteViewModel.Decimal = value.Decimal;
        _registerWriteViewModel.RegisterValue = value.Decimal;
        _registerWriteViewModel.ShowView = Visibility.Visible;
    }

    /// <summary>
    /// 接收到报文
    /// </summary>
    /// <param name="transmit">报文传输</param>
    private void Received(MessageTransmit transmit)
    {
        byte code = transmit.Protocol switch
        {
            ProtocolType.Tcp => transmit.Data[7],
            _ => transmit.Data[1]
        };

        if (transmit.Function != FunctionType.WriteSingle && transmit.Function != FunctionType.WriteMultiple || code > 80)
        {
            return;
        }

        NotifyHelper.Instance.Success("寄存器写入成功");
        _registerWriteViewModel.ShowView = Visibility.Collapsed;
    }

    /// <summary>
    /// 套接字状态改变事件
    /// </summary>
    /// <param name="status">套接字状态</param>
    private void StatusChanged(SocketStatus status)
    {
        if (status == SocketStatus.Normal)
        {
            _registerWriteViewModel.ShowView = Visibility.Collapsed;
        }
    }
}